package pl.diagnoser.server.persistance.dao;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import java.util.TreeSet;
import org.hibernate.Session;
import org.hibernate.criterion.Expression;
import org.hibernate.criterion.Restrictions;
import pl.diagnoser.client.dto.AttributeDictDTO;
import pl.diagnoser.client.dto.AttributeValuesDictDTO;
import pl.diagnoser.server.persistance.map.App;
import pl.diagnoser.server.persistance.map.AttributeDict;
import pl.diagnoser.server.persistance.map.AttributeValuesDict;
import pl.diagnoser.server.persistance.tools.HibernateUtil;

public class AttributeDictDao implements Serializable {

	private static final long serialVersionUID = 1L;
	
	@SuppressWarnings("unchecked")
	public List<AttributeDictDTO> getAttributeList(boolean loadValues) {
		Session session = HibernateUtil.getSessionFactory().openSession();
		session.beginTransaction();
		App app = new AppDao().getActiveApp();
		
		List<AttributeDict> attributeList = session.createCriteria(AttributeDict.class).add(Restrictions.eq("app", app)).list();
		List<AttributeDictDTO> attributeListDTO = new ArrayList<AttributeDictDTO>(attributeList != null ? attributeList.size() : 0);
		
		if(attributeList != null) {
			for (AttributeDict attribute : attributeList) {
				attributeListDTO.add(createAttributeDictDTO(attribute, loadValues));
			}
		}
		
		session.getTransaction().commit();
		session.close();
		return attributeListDTO;
	}
	
	public AttributeDict getAttribute(String attributeName, Session session) {
		AttributeDict attribute = (AttributeDict) session.createCriteria(AttributeDict.class).add(Restrictions.eq("attributeName", attributeName).ignoreCase()).uniqueResult();
		return attribute;
	}
	
	public ArrayList<String> getAttributeValues(String attributeName) {
		ArrayList<String> attributeValues = new ArrayList<String>();
		Session session = HibernateUtil.getSessionFactory().openSession();
		session.beginTransaction();
		AttributeDict attribute = getAttribute(attributeName, session);
		for(AttributeValuesDict attributeValue : attribute.getAttributeValuesDict()) {
			//attributeValues.add(attributeValue.getAttributeValue().replace("_", " "));
			attributeValues.add(attributeValue.getAttributeValue());
		}
		session.getTransaction().commit();
		session.close();
		
		return attributeValues;
	}
	
	public String saveAttribute(String attribute, int isDate, int isMultiple, int isFile) {
		String ret = "OK";
		Session session = HibernateUtil.getSessionFactory().openSession();
		session.beginTransaction();
	    try {
	    	App app = new AppDao().getActiveApp();
	    	session.save(new AttributeDict(attribute, isDate, isMultiple, isFile, app));
	    	session.getTransaction().commit();
	    } catch(Exception e) {
	    	ret = e.getMessage();
	    } finally {
			session.close();
	    }   
		return ret;
	}
	
	public String removeAttribute(String attribute) {
		String ret = "OK";
		Session session = HibernateUtil.getSessionFactory().openSession();
		session.beginTransaction();
	    try {
	    	App app = new AppDao().getActiveApp();
	    	AttributeDict tmpAttribute = (AttributeDict) session.createCriteria(AttributeDict.class).add(Expression.eq("attributeName", attribute)).add(Expression.eq("app", app)).uniqueResult();
	    	session.delete(tmpAttribute);
	    	session.getTransaction().commit();
	    } catch(Exception e) {
	    	ret = e.getMessage();
	    } finally {
			session.close();
	    }   
		return ret;
	}
	
	public AttributeDictDTO createAttributeDictDTO( AttributeDict attributeDict, boolean loadValues ) {
		Set<AttributeValuesDictDTO> attributeValuesDTO = new HashSet<AttributeValuesDictDTO>();
		
		if(loadValues)
			for(AttributeValuesDict attributeValues : attributeDict.getAttributeValuesDict())
				attributeValuesDTO.add(new AttributeValuesDictDTO(attributeValues.getAttributeValuesID(), attributeValues.getAttributeValue()));
		
		return new AttributeDictDTO( attributeDict.getAttributeID(),
				                     attributeDict.getAttributeName(), 
									 attributeDict.getIsDate(),
									 attributeDict.getIsMultiple(),
									 attributeDict.getIsFile(),
									 //attributeValuesDTO );
									 new TreeSet<AttributeValuesDictDTO>(attributeValuesDTO));
	}
}
